package main;

import java.io.IOException;
import java.rmi.NotBoundException;
import java.sql.SQLException;
import java.util.Properties;

import tool.GetMasterApi;
import tool.GetProper;
import upDownLoad.UpDownLoad;
import dataStore.UserDataStoreApiInterface;
import dataStruct.*;

public class UserMain {

	public static void main(String[] args) throws NumberFormatException,
			NotBoundException, IOException, SQLException, InterruptedException {
		if (args.length != 2) {
			System.out
					.println("Usage: java main.class inputFilePath outFilePath");
			return;
		}

		userProper = GetProper.get_properties("conf/config.ini");
		masterDSApi = GetMasterApi.get_masterDSApi(userProper);
		user = new User(userProper.getProperty("userName"),
				userProper.getProperty("userPassword"));

		System.out.println(user.name + " " + user.password);
		if (masterDSApi.login(user) == false) {
			System.out
					.println("slaver login failed: please check yur userName and password");
			return;
		} else {
			System.out.println("slaver login success");
		}
		UpDownLoad upDownLoad = new UpDownLoad(masterDSApi, user);

		int oper = 0;
		oper |= 7;
		/*
		 * upload file to clund
		 */
		if ((oper & 1) != 0) {
			if (upDownLoad.upload_file(args[0], args[1]) == false) {
				System.out.println(String.format("error: upload %s to %s ",
						args[0], args[1]));
				return;
			}
			System.out.println(String.format("finished :upload %s to %s",
					args[0], args[1]));

			System.out
					.println("----------------------------------------------------------------------------");
		}
		/*
		 * download file from clund
		 */
		if ((oper & 2) != 0) {
		     for(int i = 15; i > 0 ; --i)
             {
                     System.out.println(String.format("after  %d seconds will start download %s from %s", i, args[1], args[0] + ".download"));
                     Thread.sleep(1000);
             }
			System.out.println(String.format("start downloading %s from %s",
					args[1], args[0] + ".download"));
			if (upDownLoad.download_file(args[1], args[0] + ".download") == false) {
				System.out.println(String.format("Error: download %s from %s",
						args[1], args[0] + ".download"));
				return;
			}
			System.out.println(String.format("finished download %s from %s",
					args[1], args[0] + ".download"));
		}
		/*
		 * map and reduce file
		 */
		if ((oper & 4) != 0) {
		     for(int i = 15; i > 0 ; --i)
             {
                     System.out.println(String.format("after  %d seconds will start download %s from %s", i, args[1], args[0] + ".wordcount"));
                     Thread.sleep(1000);
             }
			System.out
					.println("----------------------------------------------------------------------------");
			System.out.println(String.format("start mapreduce  %s from %s",
					args[1], args[0] + ".wordcount"));
			if (upDownLoad.map_reduce(args[1], args[0] + ".wordcount") == false) {
				System.out.println(String.format("Error: mapreduce %s from %s",
						args[1], args[0] + ".wordcount"));
				return;
			}
			System.out.println(String.format("finished mapreduce  %s from %s",
					args[1], args[0] + ".wordcount"));
		}
		return;
	}

	private static User user;
	private static Properties userProper;
	private static UserDataStoreApiInterface masterDSApi;
}
